<?php 
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Model_main extends Model
{

  function __construct()
  {
    $this->Model_main();
  }
  
  function Model_main()
  {
    parent::Model();
  }
  
  function get_hostbindings() {
    log_message('debug', "model_main::hostbindings:: function starting");
    $dbr = $this->load->database('read', TRUE);
    $sql0 = "select t1.id, t1.host_list_hostname, t1.host_list_ipaddress, t1.host_list_uuid, t1.template_list_id as h_template_list_id, t2.id as template_list_id, t2.template_list_name, t2.template_list_desc  from host_list as t1, template_list as t2 where t1.template_list_id = t2.id or t1.template_list_id = '0' group by t1.host_list_hostname order by t1.host_list_hostname;";
    qstart();
    $query = $dbr->query($sql0);
    qend();
    if($query->num_rows() > 0) {      
      return $query->result_array();
    }
    else {
      $data=array(array("id" => "NULL",
                         "host_list_hostname" => "NULL",
                         "host_list_ipaddress" => "NULL",
                         "host_list_uuid" => "NULL",
                         "h_template_list_id" => "NULL",
                         "template_list_id" => "NULL",
                         "template_list_name" => "NULL",
                         "template_list_desc" => "NULL"));
      return $data;
    }
  }

  function get_commandbindings() {
    log_message('debug', "model_main::commandbindings:: function starting");
    $dbr = $this->load->database('read', TRUE);
    $sql0 = "select t1.id as command_objects_id, t1.command_objects_name, t1.command_objects_desc, t1.command_objects_command, t2.id as template_list_id, t2.template_list_name, t2.template_list_desc, t3.id as template_command_objects_map_id, t3.template_list_id, t3.command_objects_id from command_objects as t1, template_list as t2, template_command_objects_map as t3 WHERE t3.command_objects_id = t1.id AND t3.template_list_id = t2.id;";
    qstart();
    $query = $dbr->query($sql0);
    qend();
    if($query->num_rows() > 0) {
      return $query->result_array();
    }
    else {
      $data=array(array("command_objects_id" => "NULL",
			"command_objects_name" => "NULL",
			"command_objects_desc" => "NULL",
			"command_objects_command" => "NULL",
			"template_list_id" => "NULL",
			"template_list_name" => "NULL",
			"template_list_desc" => "NULL",
			"template_command_objects_map_id" => "NULL",
			"template_list_id" => "NULL",
			"command_objects_id" => "NULL"));			
      return $data;
    }
  }

  function get_command_list() {
    log_message('debug', "model_main::get_command_list starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select * from command_objects order by command_objects_name;";
    log_message('debug', "$sql");
    qstart();
    $query = $dbr->query($sql);
    qend();
    if($query->num_rows() > 0) {
      return $query->result_array();
    }
    else {
      $data=array(array("id" => "NULL",
			"command_objects_name" => "NULL",
			"command_objects_desc" => "NULL",
			"command_objects_command" => "NULL"));
      return $data;
    }
  }

  function get_template_list() {
    log_message('debug', "model_main::get_template_list starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select * from template_list order by template_list_name;";
    log_message('debug', "$sql");
    qstart();
    $query = $dbr->query($sql);
    qend();
    if($query->num_rows() > 0) {
      return $query->result_array();
    }
    else {
      $data=array(array("id" => "NULL",
                         "template_list_name" => "NULL",
                         "template_list_desc" => "NULL"));
      return $data;
    }
  }

  function get_host($host_list_id) {
    log_message('debug', "model_main::get_host starting.");
    $dbr = $this->load->database('read', TRUE);
    $sql = "select t1.*, t2.template_list_name from host_list as t1, template_list as t2 where t1.template_list_id = t2.id and t1.id = '$host_list_id';";
    log_message('debug', "$sql");
    qstart();
    $query = $dbr->query($sql);
    qend();
    if($query->num_rows() > 0) {
      return $query->result_array();
    }
    else {
      $data=array(array("id" => "NULL",
			"host_list_name" => "NULL",
			"template_list_id" => "NULL",
			"template_list_name" => "NULL"));
      return $data;
    }
  }

}
?>